#!/usr/bin/env python

import hashlib
import getpass
import sys

"""
Author:         Jkuhn
Filename:       adminTool.py
Description:    This tool allows the admin to quickly add a new account to the
                bbs system. Future improvements might include a way to 
                detect if a user already has an account, and update that hash
                rather than just adding in a new one (and leaving the user
                with two passwords.  
"""

def main():
    """ 
    This is the admin tool.  Use it to add new
    users to the BBS system.
    """
    passwordFilePath = ".passwd"

    usage = "Usage: " + sys.argv[0] + " [OPT PASSWORD FILE]"

    if (len(sys.argv) == 2):
        passwordFilePath = sys.argv[1]
    if (len(sys.argv) > 2):
        print usage
        exit()

    try:
        passwordFile = open(passwordFilePath, 'a')        
    except:
        print "Could not open " + passwordFilePath
        exit()    

    username = raw_input("Please enter the new username: ")
    pass1 = getpass.getpass("Enter a password: ")
    pass2 = getpass.getpass("Retype password: ")

    if (pass1 == pass2):
        #print "New user:", username, pass1, pass2
        writeUserEntry(username, pass1, passwordFile)
        passwordFile.close()
    else:
        print "Passwords did not match"

def writeUserEntry(user, pwd, passwordFile):
    hashedPwd = hashlib.sha256(pwd).hexdigest()
    entry = user + ":" + hashedPwd + "\n"
    passwordFile.write(entry)

if __name__=="__main__":
    main()
